//
//  DXMLRuleSetObject.m
//  DerbyStatter
//
//  Created by glenn andreas on 1/6/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DXMLRuleSetObject.h"

// defaults are MAR2014
@implementation DXMLRuleSetObject

+ (DXMLRuleSetObject *) defaultRuleSet; // MAR2014
{
    return [self ruleSetForName:@"MAR2014"];
}
+ (DXMLRuleSetObject *) ruleSetForName: (NSString *) name
{
    NSArray *parts = [name componentsSeparatedByString:@"/"];
    DXMLRuleSetObject *retval = [[DXMLRuleSetObject alloc] init];
    if ([[parts objectAtIndex:0] isEqualToString:@"MAR2014"]) {
    } else if ([[parts objectAtIndex:0] isEqualToString:@"JUN2013"]) {
        retval.officalReviewRetained = NO;
        retval.officialReviewMaximum = 1;
    } else {
        return nil;
    }
    if ([parts count] > 1) {
        NSScanner *scanner = [NSScanner scannerWithString:[parts objectAtIndex:1]];
        while (![scanner isAtEnd]) {
            NSInteger value;
            if ([scanner scanInteger:&value]) {
                if ([scanner scanString:@"p" intoString:nil]) { // period length
                    retval.period = 60 * value; // this is in minutes
                } else if ([scanner scanString:@"b" intoString:nil]) { // box trip duration
                    retval.penalty = value;
                } else if ([scanner scanString:@"f" intoString:nil]) { // foul out at...
                    retval.penaltyLimit = value;
                } else if ([scanner scanString:@"t" intoString:nil]) { // time out count
                    retval.timeoutCount = value;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
    return retval;
}

DXMLINTATTRDEFAULT(periodCount, PeriodCount, 2)
DXMLINTATTRDEFAULT(timeoutCount, TimeoutCount, 3)
DXMLINTATTRDEFAULT(officialReviewCount, OfficialReviewCount, 1)
DXMLINTATTRDEFAULT(officialReviewMaximum, OfficialReviewMaximum, 2)
DXMLBOOLATTRDEFAULT(officalReviewRetained, OfficalReviewRetained, YES)
DXMLINTATTRDEFAULT(penaltyLimit, PenaltyLimit, 7)
DXMLBOOLATTRDEFAULT(minors, Minors, NO)
DXMLINTATTRDEFAULT(minorsPerMajor, MinorsPerMajor, 4)
// length in seconds
DXMLINTATTRDEFAULT(period, Period, 30 * 60)
DXMLINTATTRDEFAULT(jam, Jam, 2 * 60)
DXMLINTATTRDEFAULT(timeout, Timeout, 1 * 60)
DXMLINTATTRDEFAULT(lineup, Lineup, 30)
DXMLINTATTRDEFAULT(penalty, Penalty, 30)

@end
